﻿using System.Linq;
using Cms.Application.Admin.Views;
using Cms.Data;
using Cms.Domain;
using Cms.Domain.RepositoryInterfaces;

namespace Cms.Application.Admin.Presenters
{
    public class EditAdministratorGroupPresenter : BasePresenter
    {
        private readonly IEditAdministratorGroupView _view;
        private readonly Administrator _administrator;

        public EditAdministratorGroupPresenter(IEditAdministratorGroupView view)
            : this(view, new RepositoryFactory())
        {
        }

        public EditAdministratorGroupPresenter(IEditAdministratorGroupView view, IRepositoryFactory repositoryFactory)
            : base(view, repositoryFactory)
        {
            _view = view;
            _administrator = AdministratorRepository.GetAdministrator(_view.AdministratorID);
            if (!_administrator.HasPermission.EditAdministratorGroup)
                _view.RedirectToPermissionDeniedPage();
        }

        public void Initialize()
        {
            _view.SetPermissions(PermissionRepository.GetPermissions());
            if (_view.GroupID == -1)
            {
                _view.DefaultPageAccess = DefaultAccess.Deny;
                _view.DefaultCustomAdminPageAccess = DefaultAccess.Deny;
            }
            else
            {
                AdministratorGroup group = AdministratorRepository.GetGroup(_view.GroupID);
                _view.Name = group.Name;
                _view.DefaultPageAccess = group.DefaultPageAccess;
                _view.DefaultCustomAdminPageAccess = group.DefaultCustomAdminPageAccess;
            }
        }
        
        public bool HasPermission(Permission permission)
        {
            AdministratorGroup group = AdministratorRepository.GetGroup(_view.GroupID);
            return group != null && group.Permissions.FirstOrDefault(p => p.ID == permission.ID) != null;
        }

        public void Save()
        {
            if (_view.Name.Trim() == "")
                AddValidationError(GetText(_view, "NoNameError"));
            if (!IsValid)
                return;
            AdministratorGroup group = _view.GroupID == -1 ? new AdministratorGroup() : AdministratorRepository.GetGroup(_view.GroupID);
            group.Name = _view.Name;
            group.DefaultPageAccess = _view.DefaultPageAccess;
            group.DefaultCustomAdminPageAccess = _view.DefaultCustomAdminPageAccess;
            foreach (Permission permission in group.Permissions)
                permission.Groups.Remove(group);
            group.Permissions.Clear();
            foreach (int permissionID in _view.SelectedPermissionIDs)
            {
                Permission permission = PermissionRepository.GetPermission(permissionID);
                permission.Groups.Add(group);
                group.Permissions.Add(permission);
            }
            AdministratorRepository.SaveGroup(group);
            _view.SetGroupID(group.ID);
        }
    }
}